package com.guigu.servlet;

import com.google.gson.Gson;
import com.guigu.dao.BookDao;
import com.guigu.mapper.BookMapper;
import com.guigu.util.BookMapperFactory;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet("/QueryBook.do")
public class QueryBookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = URLDecoder.decode(req.getParameter("name"), "utf-8");
        System.out.println(name);
        String press = req.getParameter("press");
        String author = req.getParameter("author");
        BookMapperFactory bmf = new BookMapperFactory();
        BookMapper bm = bmf.getMapper();

        if (name != null || press != null || author != null) {
            Gson gson = new Gson();
            String json = gson.toJson(bm.queryBy(name, press, author));
            bmf.closeSession();
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print(json);
        } else {
            System.out.println("参数都为空");
        }
    }
}
